package com.zn.service.impl;

import com.zn.entity.AppCustomer;
import com.zn.entity.BankInfo;
import com.zn.entity.CompanyBankInfo;
import com.zn.entity.CustomerBankInfo;
import com.zn.mapper.AppCustomerMapper;
import com.zn.mapper.BankInfoMapper;
import com.zn.mapper.CompanyBankInfoMapper;
import com.zn.mapper.CustomerBankInfoMapper;
import com.zn.service.CustomerBankInfoService;
import com.zn.util.UUIDUtils;
import com.zn.vo.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by yinjiawei on 2018/8/2.
 */
@Service
public class CustomerBankInfoServiceImpl implements CustomerBankInfoService {
    @Autowired
    private CustomerBankInfoMapper customerBankInfoMapper;
    @Autowired
    private AppCustomerMapper appCustomerMapper;
    @Autowired
    private BankInfoMapper bankInfoMapper;
    @Autowired
    private CompanyBankInfoMapper companyBankInfoMapper;

    @Transactional
    @Override
    public ResponseEntity<Message> customerBindBankCard(CustomerBankInfo customerBankInfoDto) {
        AppCustomer appCustomer=appCustomerMapper.selectByPrimaryKey(customerBankInfoDto.getCustomerId());
        if(appCustomer==null){
            //用户不存在
            return ResponseEntity.ok(Message.CUSTOMER_NOT_EXIST);
        }
        CustomerBankInfo customerBankInfo=customerBankInfoMapper.selectByCustomerIdAndBankNo(customerBankInfoDto.getCustomerId(), customerBankInfoDto.getBankNo());
        if(customerBankInfo!=null){
            //重复绑卡
            return ResponseEntity.ok(Message.TIED_CARD_REPEAT);
        }
        customerBankInfoDto.setBankInfoId(UUIDUtils.getUuid());
        customerBankInfoMapper.insertSelective(customerBankInfoDto);

        //第一次绑卡
        if(appCustomer.getBindCardStatus()==0){
            AppCustomer updateBindCardStatus=new AppCustomer();
            updateBindCardStatus.setCustomerId(appCustomer.getCustomerId());
            updateBindCardStatus.setBindCardStatus(1);
            appCustomerMapper.refreshCapital(updateBindCardStatus);
        }
        return ResponseEntity.ok(Message.REQ_SUCCESS);
    }

    @Override
    public List<CustomerBankInfo> selectByCustomerId(String customerId) {
        return customerBankInfoMapper.selectByCustomerId(customerId);
    }
    @Transactional
    @Override
    public ResponseEntity<Message> unbindBankCard(CustomerBankInfo customerBankInfoDto) {
        AppCustomer appCustomer=appCustomerMapper.selectByPrimaryKey(customerBankInfoDto.getCustomerId());
        if(appCustomer==null){
            //用户不存在
            return ResponseEntity.ok(Message.CUSTOMER_NOT_EXIST);
        }
        customerBankInfoMapper.unbindBank(customerBankInfoDto);
        //解绑之后在查询是否还存在绑定的银行卡
        List<CustomerBankInfo> bankInfoList=customerBankInfoMapper.selectByCustomerId(customerBankInfoDto.getCustomerId());
        //无绑定银行卡 修改客户卡状态为未绑定
        if(bankInfoList.size()==0){
            AppCustomer updateBindCardStatus=new AppCustomer();
            updateBindCardStatus.setCustomerId(appCustomer.getCustomerId());
            updateBindCardStatus.setBindCardStatus(0);
            appCustomerMapper.refreshCapital(updateBindCardStatus);
        }else{
            /**置为未选中**/
            customerBankInfoMapper.unSelectBank(customerBankInfoDto);
            customerBankInfoMapper.selectBank(bankInfoList.get(0));
        }
        return ResponseEntity.ok(Message.REQ_SUCCESS);
    }

    @Override
    public List<BankInfo> selectAllBankInfo() {
        List<BankInfo> bankInfoList=bankInfoMapper.selectAllBankInfo();
        return bankInfoList;
    }

    @Override
    public CompanyBankInfo getCompanyBankInfo() {
        return companyBankInfoMapper.selectByStatus();
    }
    @Transactional
    @Override
    public void customerSelectBank(CustomerBankInfo customerBankInfo) {
        //选中前 先将客户所有的卡置为未选中
        customerBankInfoMapper.unSelectBank(customerBankInfo);
        //在将当前的选中的卡 设为已选中
        customerBankInfoMapper.selectBank(customerBankInfo);
    }
}
